var Article = {
	SERVICEID : 1,
	STATE: {UNLOCKED:0, LOCKED:1},
	/* *
	 *	{
	 * 		id:1,
	 *		title:'test',
	 *		state: 0,
	 *		wrote_at: '2006-01-00 14:55',
	 *		content: null, // we do not load content at first, only load it when we need.
	 *		category: 1// The category id of the article.
	 *		isChanged: true/false
	 *	}
	 */
	articles:[]
};
Article.create = function( title, category, content, handler ){
	ranidae.api.article.create(title, category, content, function( resultCode ){
		var articleInfo =
        {
            id:resultCode,
            title:title,
            state:Article.STATE.UNLOCKED,
            wroteAt: now(),
            content: content,
            category: category,
            isChanged:false
        }
        Article.articles.push(articleInfo);
		var catIndex = Article.category.getIndex(category);
		Article.category.categories[catIndex].sonCount ++;
		handler(resultCode);
	});
}
Article.update = function( article, title, category, content ){
	var art = Article.getIndex(article);
	if ( null == art )
		throw ( 'Could not find article ID ' + article );
		
	if ( art.state == Article.STATE.LOCKED )
		throw ('Could not lock locked article! : ID:' + article );
		
	ranidae.api.article.update( article, title, category, content, function(resultCode){
		var index = Article.getIndex(article);
		Article.articles[index].title = title;
		Article.articles[index].category = category;
		Article.articles[index].content = content;
		handler(resultCode);
	});
}
Article.getIndex = function( articleId ){
	var index = Article.articles.length;
	
	while ( -1 < --index )
		if ( Article.articles[index].id == articleId )
			break;
	
	if ( -1 == index )
		throw ('Could not find article ID! : ID:' + article );
		
	return index;
}
Article.getContent =function( article, handler ){
	var art = Article.getInfo(article);
	if ( null == art )
		throw('Could not find article ID. ID:'+article);
		
	if ( null != art.content )
		handler(art.content);
	else
		ranidae.api.article.content(article, function( content ){
			var index = Article.getIndex(article);
			Article.articles[index].content = content;
			handler(content);
		});
}
Article.getInfo = function( articleId ){
	var infoIndex = Article.articles.length;
	
	while ( -1 < --infoIndex )
		if ( Article.articles[infoIndex].id == articleId*1 )
			break;
			
	return -1 == infoIndex ? null : Article.articles[infoIndex];
}
Article.lock = function( articleId, handler ){
	var art = Article.getInfo(articleId);
	if ( null == art )
		throw ('Article ID could not find : ID:' + articleId );
	
	if ( art.state == Article.STATE.LOCKED )
		throw ('Could not lock locked article! : ID:' + articleId );
		
	ranidae.api.article.lock(articleId, function( resultCode ){
		var index = Article.getIndex(articleId);
		Article.articles[ index ].state = Article.STATE.LOCKED;
		handler(resultCode);
	});
}
Article.listByCategory = function( categoryId, handler ){
    var list = Article.getList(categoryId);

	if ( null != list )
		handler(list);
	else
        ranidae.api.article.list(categoryId, function( articleList ){
            Article.addToList(articleList);
            var index = Article.category.getIndex(categoryId);
			Article.category.categories[index].isLoaded = true;
			handler(articleList);
        });
}
Article.getList = function( categoryId ){
	var cat = Article.category.getInfo(categoryId);
    if ( null == cat )
       throw ('Category ID could not find. ID: ' + categoryId);
	
	if ( !cat.isLoaded )
	   return null;
	   
	var articleList = [];
	for ( var i=0; i<Article.articles.length; i++ )
		if ( Article.articles[i].id == categoryId * 1 )
			articleList.push(Article.articles[i]);
			
	return articleList;
}
Article.addToList = function( articleList ){
    for ( var i=0; i<articleList.length; i++ ){
        var article =
        {
            id:articleList[i].id,
            title:articleList[i].title,
            state:articleList[i].state,
            wroteAt: articleList[i].wrote_at,
            content: null,
            category: articleList[i].category,
            isChanged:false
        }
        Article.articles.push(article);
    }
}
Article.downloadById = function( article, handler ){
	var art = Article.getInfo(article);
	if ( null == art )
		throw ('Article ID could not find : ID:' + article );
		
	ranidae.api.article.download.byId(article, function(result){
		handler(result.url);
	});
}
Article.downloadByCategory = function( category, handler ){
	var cat = Article.category.getInfo(category);
	if ( null == cat )
		throw ('category ID could not find : ID:' + category );
		
	ranidae.api.article.download.byCategory(category, function(result){
		handler(result.url);
	});
}
Article.move = function( articleId, categoryId, handler ){
	var artIndex = Article.getIndex(articleId);
	var catIndex = Article.category.getIndex(categoryId);
	
	ranidae.api.article.move(articleId, categoryId, function(resultCode){
		var oldCategoryIndex = Article.category.getIndex(Article.articles[artIndex].category);
		Article.articles[artIndex].category = categoryId;
		Article.category.categories[catIndex].sonCount ++;
		Article.category.categories[oldCategoryIndex].sonCount --;
		
		handler(resultCode);
	});
}
Article.category = new Category(Article.SERVICEID);